package com.example.prolyric.database;

import android.content.Context;
import android.database.Cursor;
import android.util.Log;

public class DatabaseOffice {
	private Cursor mCusor;
	private SqliteServices service = null;
	public DatabaseOffice(Context context) {
		service = new SqliteServices(context);
	}
	
	/*
	 * insert a lyric to database
	 * */
	public boolean insertLyric(LyricDTO l)
	{
		boolean check = false;
		String sql = "insert into lyric(songname,singer,lyricEn,lyricVi,link,created) values" +
				"('"+l.getSongName()+"','"+l.getSinger()+"','"+l.getLyricEn()+"','"+l.getLyricVi()+"','"+l.getLyricEn()+"','"+l.getCreated()+"')";
		try {
			service.exeSQL(sql);
			check = true;
		} catch (Exception e) {
			Log.d("Error Database", "Error insert Lyric");
		}
		finally
		{
			service.close();
		}
		
		return check;
	}
	
	public LyricDTO getLyricById(int id)
	{
		LyricDTO l = null;
		String sql = "select * from lyric where id = " + id;
		mCusor = service.queryData(sql);
		if(mCusor.moveToFirst())
		{
			l = new LyricDTO();
			l.setId(mCusor.getInt(mCusor.getColumnIndex("id")));
			l.setSongName(mCusor.getString(mCusor.getColumnIndex("songname")));
			l.setSinger(mCusor.getString(mCusor.getColumnIndex("singer")));
			l.setLyricEn(mCusor.getString(mCusor.getColumnIndex("lyricEn")));
			l.setLyricVi(mCusor.getString(mCusor.getColumnIndex("lyricVi")));
			l.setLink(mCusor.getString(mCusor.getColumnIndex("link")));
			l.setCreated(mCusor.getString(mCusor.getColumnIndex("created")));
			
		}
		return l;
	}
	
	public LyricDTO getLyricByName(String songname)
	{
		LyricDTO l = null;
		String sql = "select * from lyric where songname = '"+songname+"'" ;
		mCusor = service.queryData(sql);
		if(mCusor.moveToFirst())
		{
			l = new LyricDTO();
			l.setId(mCusor.getInt(mCusor.getColumnIndex("id")));
			l.setSongName(mCusor.getString(mCusor.getColumnIndex("songname")));
			l.setSinger(mCusor.getString(mCusor.getColumnIndex("singer")));
			l.setLyricEn(mCusor.getString(mCusor.getColumnIndex("lyricEn")));
			l.setLyricVi(mCusor.getString(mCusor.getColumnIndex("lyricVi")));
			l.setLink(mCusor.getString(mCusor.getColumnIndex("link")));
			l.setCreated(mCusor.getString(mCusor.getColumnIndex("created")));
			
		}
		return l;
	}

}
